<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" th:href="@{/res/css/amazeui.min.css}"/>
    <link rel="stylesheet" th:href="@{/res/css/admin.css}"/>
    <script th:src="@{/js/jquery-1.11.3.min.js}"></script>
    <script th:src="@{/myplugs/js/plugs.js}"></script>
</head>

<body>
<div class="admin-content-body">
    <div class="am-cf am-padding am-padding-bottom-0">
        <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">数据字典管理</strong>
            <small></small>
        </div>
    </div>

    <hr>

    <div class="am-g">
        <div class="am-u-sm-12 am-u-md-6">
            <div class="am-btn-toolbar">
                <div class="am-btn-group am-btn-group-xs">
                    <button type="button" class="am-btn am-btn-default" onclick="add()"><span
                            class="am-icon-plus"></span> 新增
                    </button>
                </div>
            </div>
        </div>
        <div class="am-u-sm-12 am-u-md-3">

        </div>
        <div class="am-u-sm-12 am-u-md-3">
            <div class="am-input-group am-input-group-sm">
                <input type="text" placeholder="请输入要查找的字典类型" id="search" class="am-form-field">
                <span class="am-input-group-btn">
            <button class="am-btn am-btn-default" type="button" onclick="search()">搜索</button>
          </span>
            </div>
        </div>
    </div>


    <div class="am-g">
        <div class="am-u-sm-12">
            <form class="am-form">
                <table class="am-table am-table-striped am-table-hover table-main">
                    <thead>
                    <tr class="tablecss">
                        <!--<th class="table-check"><input type="checkbox"></th>-->
                        <th class="table-id">序号</th>
                        <th class="table-type">字典类型</th>
                        <th class="table-type">用途描述</th>
                        <th class="table-type">字典编码</th>
                        <th class="table-type">编码值</th>
                        <th class="table-set">操作</th>
                    </tr>
                    </thead>

                    <tbody id="mytable"></tbody>
                </table>

                <div class="am-cf">
                    共<a><span id="listcount"></span></a>条记录
                    <!--<div class="am-fr">
                        <ul class="am-pagination">
                            <li class="am-disabled">
                                <a href="#">«</a>
                            </li>
                            <li class="am-active">
                                <a href="#">1</a>
                            </li>
                            <li>
                                <a href="#">2</a>
                            </li>
                            <li>
                                <a href="#">3</a>
                            </li>
                            <li>
                                <a href="#">»</a>
                            </li>
                        </ul>
                    </div>-->
                </div>
                <hr>
            </form>
        </div>
    </div>
</div>
</body>

<script type="text/javascript">
    //保存查询的结果列表
    var list="";

    //页面数据初始化
    $(function () {
        var $tbody = $("#mytable");//表格
        $.ajax({
            type: 'post',
            url: '/dictionary/getDicList',
            data: {},
            dataType: 'json',
            success: function (mydata) {
                list=mydata;
                // console.log(mydata);
                // console.log(list);
                var newHtml = '';
                $("#listcount").html(" " + mydata.length + " ");
                for (i = 0; i < mydata.length; i++) {
                    newHtml += '<tr class="low-'+ i +'">';
                    newHtml += '  <td>' + parseInt(i+1) + '</td>';
                    newHtml += '  <td>' + mydata[i].dictType + '</td>';
                    newHtml += '  <td>' + mydata[i].dictDescription + '</td>';
                    newHtml += '  <td>' + mydata[i].code + '</td>';
                    newHtml += '  <td>' + mydata[i].codeValue + '</td>';
                    newHtml += '  <td>';
                    newHtml += '      <div class="am-btn-toolbar">';
                    newHtml += '          <div class="am-btn-group am-btn-group-xs">';
                    newHtml += '              <a href="#" class="am-btn am-btn-default" onclick="edit('+i+')"><span class="am-icon-pencil-square-o"></span> 编辑 </a>';
                    newHtml += '              <a href="#" class="am-btn am-btn-default" onclick="del('+i+')"><span class="am-icon-trash-o"></span> 删除 </a>';
                    newHtml += '          </div>';
                    newHtml += '      </div>';
                    newHtml += '  </td>';
                    newHtml += '</tr>';
                }
                $tbody.html(newHtml);
            },
            error: function () {
                alert("error");
            }
        });
    })

    //编辑动态表格的每一条记录
    function edit(i) {
        // var arr = $(".low-"+i).children('td');
        //var id=arr[1].outerText;
        var id=list[i].id;
        var type=list[i].dictType;
        var desc=list[i].dictDescription;
        var code=list[i].code;
        var value=list[i].codeValue;
        $.jq_Panel({
            title: "修改数据字典",
            iframeWidth: 500,
            iframeHeight: 300,
            url: "/editDict?id="+id+"&type="+type+"&description="+desc+"&code="+code+"&value="+value
        });
    }

    //新增记录
    function add() {
        $.jq_Panel({
            title: "新增数据字典",
            iframeWidth: 500,
            iframeHeight: 300,
            url: "/addDict"
        });
    }

    //删除动态表格的一条记录
    function del(i){
        var id=list[i].id;
        $.ajax({
            type: 'get',
            url: '/dictionary/delete',
            data: {id:id},
            dataType: 'text',
            success: function (mydata) {
                alert(mydata);
            },
            error: function () {
                alert("请求失败");
            }
        });
        //刷新页面
        window.location.reload();
    };

    //查找符合条件的记录
    function search() {
        //表格
        var $tbody = $("#mytable");

        //获取搜索条件
        var type=document.getElementById("search").value;

        $.ajax({
            type: 'post',
            url: '/dictionary/selectByType',
            data: {type:type},
            dataType: 'json',
            success: function (mydata) {
                list=mydata;
                var newHtml = '';
                $("#listcount").html(" " + mydata.length + " ");
                for (i = 0; i < mydata.length; i++) {
                    newHtml += '<tr class="low-'+ i +'">';
                    newHtml += '  <td>' + parseInt(i+1) + '</td>';
                    newHtml += '  <td>' + mydata[i].dictType + '</td>';
                    newHtml += '  <td>' + mydata[i].dictDescription + '</td>';
                    newHtml += '  <td>' + mydata[i].code + '</td>';
                    newHtml += '  <td>' + mydata[i].codeValue + '</td>';
                    newHtml += '  <td>';
                    newHtml += '      <div class="am-btn-toolbar">';
                    newHtml += '          <div class="am-btn-group am-btn-group-xs">';
                    newHtml += '              <a href="#" class="am-btn am-btn-default" onclick="edit('+i+')"><span class="am-icon-pencil-square-o"></span> 编辑 </a>';
                    newHtml += '              <a href="#" class="am-btn am-btn-default" onclick="del('+i+')"><span class="am-icon-trash-o"></span> 删除 </a>';
                    newHtml += '          </div>';
                    newHtml += '      </div>';
                    newHtml += '  </td>';
                    newHtml += '</tr>';
                }
                $tbody.html(newHtml);
            },
            error: function () {
                alert("请求失败");
            }
        })
    };
</script>
</html>